home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++ / leda / 35 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.1 KB  |  151 lines

  1. Path: fs7.ece.cmu.edu!cxdunn
  2. From: cxdunn@ece.cmu.edu (Christopher Dunn)
  3. Newsgroups: comp.lang.smalltalk,comp.lang.perl.misc,comp.lang.pascal.borland,comp.lang.eiffel,comp.lang.cobol,comp.lang.c++.leda,comp.lang.c,comp.lang.basic.visual.3rdparty,alt.computer.workshop.live,alt.comp.hardware.homebuilt,alt.clearing.technology,alt.chinese.computing,news.newusers.questions
  4. Subject: Re: PROGRAMERS OF ANY LANGUAGE
  5. Date: Sun, 31 Mar 1996 17:22:55 EST
  6. Organization: Electrical and Computer Engineering, Carnegie Mellon.
  7. Distribution: cmu
  8. Message-ID: <1996Mar31.172255@nova.ece.cmu.edu>
  9. References: <Pine.SOL.3.91.960329010021.13209A-100000@harvey>
  10. NNTP-Posting-Host: nova.ece.cmu.edu
  11. X-newsreader: xrn 7.01
  12. To: Drew <aellis@harvey>
  13. Originator: cxdunn@ece.cmu.edu
  14.  
  15.  
  16. Here is a short report a produced recently to convince some
  17. people in this department that C++ is not inferior to C:
  18.  
  19.     I hope that this small example settles, once
  20. and for all, the matter of the differences 
  21. between C and C++ coding.
  22.  
  23. 1) C compiles much faster. (12 times faster here.)
  24. 2) C compiles into a much smaller executable.
  25.     (5 times smaller here.)
  26. 3) C++ requires a SMALLER source file.
  27. 4) C++ is AT LEAST AS FAST AS C.
  28.  
  29.     The reason for the speed advantage in this
  30. case is that C uses qsort, which calls a compare
  31. function via a function pointer.  C++ uses sort,
  32. which is the STL implementation of qsort, and which
  33. uses the standard < operator.
  34.     If the items being sorted were objects, then
  35. the STL sort could use an inline version of operator<,
  36. and the speed advantage of C++ would be similar.  There
  37. would be no function call overhead for comparisons in C++,
  38. while in C there would still be a function-pointer dereference
  39. and a function call (with concommitant stack operations).
  40.     The only way for C to catch up to C++ in this
  41. example is for the qsort routine to be hand-coded, eliminating
  42. the need for a comparison function.  But a primary intent of
  43. the Standard Template Library is to eliminate the need for
  44. hand-coding of generic library routines without loss of
  45. efficiency.
  46.     When people use advanced features of C++ like 
  47. polymorphism (with the "virtual" keyword) they introduce
  48. inefficiencies into their code, but the equivalent
  49. C program (which would use various look-up tables) would
  50. be no faster.  
  51.     C++ is not inherently slower than C.  With generic
  52. libraries, C++ is actually faster.  I hope this convinces you.
  53. **************************************************************
  54. nova:test/timing/ls -l
  55. total 50
  56. -rw-r--r--   1 cxdunn   CAD           66 Mar 13 16:52 Makefile
  57. -rwxr-xr-x   1 cxdunn   CAD         3684 Mar 13 16:57 csort*
  58. -rw-r--r--   1 cxdunn   CAD          491 Mar 13 16:48 csort.c
  59. -rwxr-xr-x   1 cxdunn   CAD        19155 Mar 13 16:57 sort*
  60. -rw-r--r--   1 cxdunn   CAD          331 Mar 13 16:32 sort.C
  61. nova:test/timing/time make csort
  62.         cc -O2  csort.c -o csort
  63. 0.410u 0.160s 0:00.87 65.5% 1295+508k 0+0io 19pf+0w
  64. nova:test/timing/time make sort
  65.         xlC -O2 -I/afs/ece/usr/cadmisc/C++/include/STL  sort.C -o sort
  66. 5.020u 0.560s 0:10.52 53.0% 1994+1670k 0+0io 455pf+0w
  67. nova:test/timing/time csort 1000000 123
  68. 11.680u 0.010s 0:11.68 100.0% 3+3885k 0+0io 0pf+0w
  69. nova:test/timing/time sort 1000000 123
  70. 2.630u 0.010s 0:02.63 100.3% 19+3761k 0+0io 0pf+0w
  71. **************************************************
  72. A note on the source code:
  73.     The sort() function is the STL version
  74. of qsort.  (The name qsort was already taken, of
  75. course.)
  76.     I have checked that both programs sort
  77. correctly.  I use random long-integers to 
  78. eliminate differences that might result from
  79. different choices of a pivot element, and I
  80. use a random number seed (specified on the
  81. command-line) to ensure that they sort the
  82. same list.
  83.     I used no tricks in the C++ code; it is
  84. as similar to the C code as possible.
  85.     I used -O2 optimization for both.  -O3
  86. gained another 0.1 seconds for the C++ version and
  87. nothing for the C version, with little change in
  88. code size, but longer compilation time.
  89.     I found that, for 1,000,000 elements,
  90. csort takes 0.5s for initialization, while
  91. sort (C++) takes 0.7s.  C++ generally consumes
  92. more time in initialization, though the
  93. asymptotic behavior matches or exceeds that
  94. of C.
  95.  
  96. C code:
  97. #include <stdlib.h>
  98.  
  99. int cmp( const void *vA, const void *vB )
  100.   long int *A = (long int*)vA;
  101.   long int *B = (long int*)vB;
  102.   if (*A < *B) return -1;
  103.   else if (*A > *B) return +1;
  104.   else return 0;
  105. }
  106.  
  107. int
  108. main(int argc, char *argv[])
  109. {
  110.     int N = atoi(argv[1]);
  111.     int i = N;
  112.     typedef long int * vec;
  113.     vec V = malloc( N*sizeof(long int) );
  114.     vec U = V;
  115.     if (argc>2) srand(atoi(argv[2]));
  116.     while ( i-- )
  117.         *U++ = rand()*rand();
  118.     qsort( V, N, sizeof(long int), cmp );
  119.     return( (int)V[N/4] );
  120. }
  121.  
  122. C++ code:
  123. #include <vector.h>
  124. #include <algo.h>
  125. #include <stdlib.h>
  126.  
  127. int
  128. main(int argc, char *argv[])
  129. {
  130.     int N = atoi(argv[1]);
  131.     if (argc>2) srand(atoi(argv[2]));
  132.     int i = N;
  133.     typedef vector<long int> vec;
  134.     vec V(N);
  135.     vec::iterator U = V.begin();
  136.     while ( i-- )
  137.         *U++ = rand()*rand();
  138.     sort( V.begin(), V.end() );
  139.     return( (int)V[N/4] );
  140. }
  141.  
  142. -- 
  143. Electrical and Computer Engineering
  144. Carnegie Mellon University
  145. cxdunn@ece.cmu.edu
  146. -- 
  147. Electrical and Computer Engineering
  148. Carnegie Mellon University
  149. cxdunn@ece.cmu.edu
  150.